package replicated_calculator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

/**
 *
 * Rudimentary implementation of a Textual User Interface for a 
 * DistributedCalculator client.
 *
 * @author Jesper Buus Nielsen, Aarhus University, 2012.
 *
 */

public class ClientTUI {

    synchronized public static void report(String str) {
        System.out.println(str);
    }

    public static void main(String[] args) {
        /**
         * Here you would instantiation your own, more impressive client instead.
         */


        try {
            // For reading from standard input
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String s;

            report("<var> = <int>");
            report("<var> = <var> + <var>");
            report("<var> = <var> * <var>");
            report("<var> = <var> < <var>");
            report("Print <var>");
            report("Exit: Gracefully logs out");
            report("Crash: Makes the client crash.");
            report("");

            /*
            * Read the client identifier.
            */
            String clientName;
            System.out.print("Login: ");
            if ((s = stdIn.readLine()) != null) {
                clientName = s;
            } else {
                return;
            }

            /*
            * Get the address of the server.
            */
            String serverAddress;
            System.out.print("Enter address of a server, no port given for default: ");
            if ((s = stdIn.readLine()) != null) {
                serverAddress = s;
            } else {
                return;
            }

            int port = Parameters.clientPortForServer;
            System.out.print("Enter port for this client to listen on, or ENTER for default");
            if ((s = stdIn.readLine()) != null) {
                if(!s.equals("")){
                    port = Integer.parseInt(s);
                }
            }

            Client client = new ClientNonRobust(port);

            if (client.connect(serverAddress,clientName)) {
                report("Connected");
            } else {
                report("Connection failed!");
                return;
            }

            while ((s = stdIn.readLine()) != null) {
                if (s.equals("Crash")) {
                    System.exit(-1);
                }
                else if (s.equals("Exit")) {
                    client.disconnect();
                    break;
                }
                else if (s.toLowerCase().startsWith("print")) {
                    final String var = s.substring(6).trim();
                    client.read(var, new Callback<BigInteger>() {
                        public void result(BigInteger bi) {
                            report(var + ": " + bi);
                        }});
                }
                else {
                    int equalityAt = s.indexOf("=");
                    if (equalityAt == -1) {
                        System.out.println("Bad user!");
                    }
                    else {
                        String res = s.substring(0, equalityAt).trim();
                        String rest = s.substring(equalityAt+1).trim();
                        if (rest.matches("\\p{Digit}+")) {
                            client.assign(res, new BigInteger(rest));
                        }
                        else {
                            int plusAt = rest.indexOf("+");
                            if (plusAt != -1) {
                                String left = rest.substring(0, plusAt).trim();
                                String right = rest.substring(plusAt+1).trim();
                                client.add(left, right, res);
                            }
                            else {
                                int multAt = rest.indexOf("*");
                                if (multAt != -1) {
                                    String left = rest.substring(0, multAt).trim();
                                    String right = rest.substring(multAt+1).trim();
                                    client.mult(left, right, res);
                                }
                                else {
                                    int compAt = rest.indexOf("<");
                                    if (compAt != -1) {
                                        String left = rest.substring(0, compAt).trim();
                                        String right = rest.substring(compAt+1).trim();
                                        client.compare(left, right, res);
                                    }
                                    else {
                                        System.out.println("Bad user!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Shutting down!");
    }
}
